package com.kanshu.mvp_basic.demo_user_info.view;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kanshu.mvp_basic.R;
import com.kanshu.mvp_basic.demo_user_info.presenter.IUserPresenter;
import com.kanshu.mvp_basic.demo_user_info.presenter.UserPresenterImpl;

import androidx.appcompat.app.AppCompatActivity;

/**
 * A login screen that offers login via email/password.
 */
public class UserActivity extends AppCompatActivity implements IUserView, View.OnClickListener {


    private EditText etID, etUserName, etUserPwd;
    private Button btnSave, btnRead;

    private IUserPresenter mUserPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        initView();
        mUserPresenter = new UserPresenterImpl(this);
    }

    private void initView() {
        etID = findViewById(R.id.et_id);
        etUserName = findViewById(R.id.et_name);
        etUserPwd = findViewById(R.id.et_pwd);
        Button btnSave = findViewById(R.id.btn_save);
        Button btnRead = findViewById(R.id.btn_read);
        btnSave.setOnClickListener(this);
        btnRead.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                mUserPresenter.saveUser();
                break;
            case R.id.btn_read:
                mUserPresenter.loadUser();
                break;
        }
    }

    @Override
    public int getID() {
        return Integer.parseInt(etID.getText().toString().trim());
    }

    @Override
    public String getUserName() {
        return etUserName.getText().toString().trim();
    }

    @Override
    public String getUserPassword() {
        return etUserPwd.getText().toString().trim();
    }

    @Override
    public void setUserName(String userName) {
        etUserName.setText(userName);
    }

    @Override
    public void setUserPassword(String userPassword) {
        etUserPwd.setText(userPassword);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

